/*
This file is part of vlc-remote.

vlc-remote is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

vlc-remote is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with vlc-remote.  If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 1.1
import com.meego 1.0
import "common"
import "mediaRemoteFunctions.js" as MediaRemote
import "remoteStatus.js" as RemoteStatus
import "vlcRemote.js" as VlcRemote

Page {
    id: root
     tools: commonTools
     property string server: ""
     property string serverName: "Not connected"
     property string currentItemId: ""


     Component.onCompleted: {
         screen.allowedOrientations = Screen.Portrait;
         //lockInPortrait = true;
         orientationLock = Screen.Portrait;

         MediaRemote.init(server);
     }

     onServerChanged: {
         if ("" == server) {
             pageStack.push(remotesPage);
         }
         else {
             MediaRemote.init(server);
             xmlModel.reload();
         }
     }

     onVisibleChanged:  {
         if (visible) {
             if (playlistDirty) {
                 xmlModel.reload();
                 playlistDirty = false;
             }
         }
     }

     onCurrentItemIdChanged: {
         console.debug("current item index: " + currentItemId);
     }

     Timer {
         id: timer
         interval: !platformWindow.visible ? 3600000 : screen.minimized ? playlistRefreshThumbnailed : playlistRefreshFocused;
         repeat: true
         running: true
         triggeredOnStart: true

         onTriggered: {
             xmlModel.reload();
         }
     }

     Timer {
        id:timerOneShot
        repeat: false
        triggeredOnStart: false;
        interval: 1000
        onTriggered: {
            xmlModel.reload();
        }
     }

     XmlListModel {
         id: xmlModel
         source: "http://"+server+"/requests/playlist.xml"
         query: "//leaf"

         XmlRole { name: "trackId"; query: "@id/string()"; }
         XmlRole { name: "path"; query: "@name/string()"; }
         XmlRole { name: "filename"; query: "@name/string()"; }
         XmlRole { name: "uri"; query: "@uri/string()"; isKey: true; }
         XmlRole { name: "titleName"; query: "title/string()"; }
         XmlRole { name: "artist"; query: "artist/string()"; }
         XmlRole { name: "album"; query: "album/string()"; }
         XmlRole { name: "current"; query: "@current/string()"; isKey: true; }

         onStatusChanged: {
             if ( status == XmlListModel.Loading)
                 busy.running = true;
             else busy.running = false;
         }

     }

     Component {
         id: highlight
         Rectangle {
             color: "black"
             border.width: 2
             border.color: accentColor
             radius:  8
             y: (-1 < playlistListView.currentIndex) ? playlistListView.currentItem.y : 0
             Behavior on y {
                 SpringAnimation {
                     spring: 2
                     damping: 0.2
                 }
             }
         }
     }

     Component {
         id: sectionHeader
         Rectangle {
             width: root.width
             height: childrenRect.height
             color: "black"
             border.width: 2
             border.color: accentColor
             Text {
                 font.pixelSize: 24
                 color: "white"
                 font.bold: true
                 text: section
             }
         }
     }

     ScrollDecoratorStyle {
         id: scrollDecoratorStyle
         inverted: true
     }
     ScrollDecorator {
         flickableItem: playlistListView
         platformStyle: scrollDecoratorStyle
         z: 12
     }
/*
     SectionScrollerStyle {
         id: sectionScrollerStyle
         fontBoldProperty: true
         fontPixelSize: 24
         textColor: "white"
         textColorHighlighted: accentColor
     }
     SectionScroller {
         listView: playlistListView
         platformStyle: sectionScrollerStyle
     }
*/
     ButtonStyle {
         id: vlcButtonStyle
         textColor: "white"
         background: "black"
     }

     PageHeader {
         id: pageHeader
         anchors.top: root.top
         title: qsTr("Playlist")
         subtitle: serverName + " @ " + server + ""
         icon: "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-refresh-white.png"
         onClicked: xmlModel.reload()
     }


     ListView{
         id: playlistListView
         anchors.fill: parent
         anchors.topMargin: 58
         anchors.bottomMargin: 64
         spacing: 4
         model:xmlModel
         delegate:ItemDelegate {
             title: "" + MediaRemote._unescape(MediaRemote.preferString(titleName, filename, "Unknown")) + "";
             subTitle: "<i>" + MediaRemote._unescape(album) + "</i> " + MediaRemote._unescape(artist) + "";
             iconType: MediaRemote.getListIconType(uri);
             isCurrent: ("current" == current)
             onClicked: {
                 playlistListView.currentIndex = index;
             }
             onDoubleClicked: {
                 MediaRemote.remoteRequestTrackById(trackId);
                 pageHeader.notify(MediaRemote.preferString(titleName, filename, "Unknown") + qsTr(" now playing!"), 2000);
             }
         }
         highlight: highlight
         highlightFollowsCurrentItem: true
         highlightMoveDuration: 2

         //section.property: "artist"
         //section.criteria: ViewSection.FirstCharacter
         //section.delegate: sectionHeader

     }

     QueryDialog {
         id: dialogConfirmClear
         property alias message:  message.text
         titleText: qsTr("Really clear playlist?")
         content:  Text {
             id: message
             width:  parent.width
             wrapMode: "WrapAtWordBoundaryOrAnywhere"
             maximumLineCount: 6
             font.pixelSize: 24
             color: "white"
             text: qsTr("Do you want to clear the current playlist?")
         }
         acceptButtonText: qsTr("Yes, clear it")
         onAccepted: {
             MediaRemote.remoteRequestClearPlaylist();
             timerOneShot.start();
         }
         rejectButtonText: qsTr("No, leave it")
     }

     Rectangle {
         color: "black"
         width: parent.width
         height: controlsRow.height + 16
         anchors.bottom: root.bottom
         anchors.topMargin: 0
         anchors.bottomMargin: 0

     Row {
         id: controlsRow
         height: 64
         opacity: 100
         spacing: 4
         anchors.horizontalCenter: parent.horizontalCenter
         anchors.verticalCenter: parent.verticalCenter

         Button {
             //platformStyle: vlcButtonStyle
             id: btnPlay
             enabled: (-1 < playlistListView.currentIndex)
             width: 86
             iconSource: (-1 < playlistListView.currentIndex) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-mediacontrol-play-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-mediacontrol-play-dimmed-white.png"
             //text: "|>"
             onClicked:  {
                  if (-1 < playlistListView.currentIndex) {
                      pageHeader.notify(MediaRemote.preferString(xmlModel.get(playlistListView.currentIndex).titleName, xmlModel.get(playlistListView.currentIndex).filename, "Unknown") + qsTr(" now playing!"), 2000);
                      MediaRemote.remoteRequestTrackById(xmlModel.get(playlistListView.currentIndex).trackId);

                      timerOneShot.start();
                  }
             }
         }
         Button {
             //platformStyle: vlcButtonStyle
             id: btnRemove
             enabled: (-1 < playlistListView.currentIndex)
             width: 86
             iconSource: (-1 < playlistListView.currentIndex) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-delete-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-delete-dimmed-white.png"
             //text: "<<"
             onClicked:  {
                 if (-1 < playlistListView.currentIndex) {
                     pageHeader.notify(MediaRemote.preferString(xmlModel.get(playlistListView.currentIndex).titleName, xmlModel.get(playlistListView.currentIndex).filename, "Unknown") + qsTr(" removed from playlist!"), 2000);
                     MediaRemote.remoteRequestDeleteFromPlaylist(xmlModel.get(playlistListView.currentIndex).trackId);
                     //xmlModel.reload();
                     // how to remove an item from the listview
                     playlistListView.currentItem.height = -4;
                     playlistListView.currentItem.visible = false;
                     playlistListView.incrementCurrentIndex();
                     if (xmlModel.get(playlistListView.currentIndex).isCurrent) timerOneShot.start();
                 }
             }
         }
         Button {
             //platformStyle: vlcButtonStyle
             id: btnClear
             width: 86
             iconSource: "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-cancle-white.png"
             //text: "||"
             onClicked:  {
                 dialogConfirmClear.open();
             }
         }
         Button {
             //platformStyle: vlcButtonStyle
             id: btnLoop
             width: 86
             iconSource: (repeat) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-repeat-white-selected.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-repeat-white.png"
             //text: "{}"
             onClicked:  {
                 MediaRemote.remoteRequestToggleLoop(); //remoteRequestLoopMode(repeat ? 1 : 3);
             }
         }
         Button {
             //platformStyle: vlcButtonStyle
             id: btnShuffle
             width: 86
             iconSource: (shuffle) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-shuffle-white-selected.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-shuffle-white.png"
             //text: "}{"
             onClicked:  {
                 MediaRemote.remoteRequestToggleShuffle(); //remoteRequestShuffleMode(shuffle ? 1 : 3);
             }
         }
     }
     }



     BusyIndicator {
         id:busy
         anchors.centerIn: root
         visible: running
         running: true
         platformStyle: BusyIndicatorStyle { size: "large" }
     }
}
